<?php
namespace app\payclass\controller;
use app\AdminRest;

use app\ApiRest;
use app\payclass\model\Active;
use app\payclass\model\Banner;
use app\payclass\model\Cate;
use app\payclass\model\ClassList;
use app\payclass\model\Config;
use app\payclass\model\LookRecord;
use app\payclass\model\Order;
use app\payclass\model\OrderGoods;
use app\payclass\model\Record;
use app\payclass\model\Video;
use app\shop\controller\IndexWxPay;
use app\shop\model\AdminGoods;
use app\shop\model\IndexGoods;
use think\App;

use app\payclass\model\Order as model;

use think\facade\Db;


class IndexOrder extends ApiRest
{

    protected $model;

    protected $video_model;

    protected $banner_model;

    protected $cate_model;

    protected $look_record_model;

    protected $class_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->video_model  = new Video();

        $this->banner_model = new Banner();

        $this->cate_model   = new Cate();

        $this->look_record_model = new LookRecord();

        $this->class_model = new ClassList();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-15 10:40
     * @功能说明:下单支付
     */
    public function payOrder(){

        $input = $this->_input;

        $class_info = $this->class_model->classInfo(['id'=>$input['class_id'],'status'=>1]);

        if(empty($class_info)){

            $this->errorMsg('视频已经下架');
        }
        //查询配置
        $config_model = new Config();

        $over_time = $config_model->where(['uniacid'=>$this->_uniacid])->value('order_over_time');

        $dis = [

            'class_id' => $input['class_id'],

            'user_id'  => $this->getUserId(),

            'status'   => 3
        ];

        $record_model = new Record();
        //是否参加了活动
        $true_record = $record_model->recordInfo($dis);


        $insert = [

            'uniacid'    => $this->_uniacid,

            'user_id'    => $this->getUserId(),

            'order_code' => orderCode(),

            'init_price' => !empty($true_record)?$true_record['init_price']:$class_info['price'],

            'pay_price'  => !empty($true_record)?$true_record['price']:$class_info['price'],

            'pay_type'   => 1,

            'transaction_id' => '',

            'over_time'  => $over_time+time(),

            'is_atv'     => !empty($true_record)?$true_record['id']:0,

            'staff_id'   => $input['staff_id']

        ];

        Db::startTrans();

        $res = $this->model->orderAdd($insert);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('支付失败');
        }

        $id  = $this->model->getLastInsID();

        $order_goods_insert = [

            'uniacid' => $this->_uniacid,

            'order_id'=> $id,

            'class_id'=> $class_info['id'],

            'title'   => $class_info['title'],

            'init_price' => $class_info['original_price'],

            'price'   => $class_info['price'],

            'num'     => 1,

            'cover'   => $class_info['cover']
        ];

        $order_goods = new OrderGoods();

        $res = $order_goods->insert($order_goods_insert);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('支付失败');
        }

        Db::commit();
        //微信支付
        if($insert['pay_price']>0){

            $pay_controller = new IndexWxPay($this->app);

            $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$this->getUserInfo()['openid'],$this->_uniacid,"购买课程",['type' => 'PayClass' , 'out_trade_no' => $insert['order_code']],$insert['pay_price']);

        }else{
            //直接回调
            $jsApiParameters = $this->model->orderResult($insert['order_code'],$insert['order_code']);

        }

        return $this->success($jsApiParameters);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-21 13:44
     * @功能说明:重新付款
     */
    public function rePay(){

        $input = $this->_input;

        $order = $this->model->orderInfo(['id'=>$input['id']]);

        if(empty($order)){

            $this->errorMsg('订单未找到');
        }

        if($order['pay_type']!=1){

            $this->errorMsg('订单状态错误');

        }

        $pay_controller = new IndexWxPay($this->app);

        $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$this->getUserInfo()['openid'],$this->_uniacid,"购买课程",['type' => 'PayClass' , 'out_trade_no' => $order['order_code']],$order['pay_price']);

        return $this->success($jsApiParameters);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-15 14:32
     * @功能说明:用户订单列表
     */
    public function userOrderList(){

        $this->model->check($this->_uniacid);

        $input = $this->_input;

        $dis = [

            'a.uniacid' => $this->_uniacid,

            'a.user_id' => $this->getUserId()
        ];

        $data = $this->model->orderList($dis,10);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-15 14:34
     * @功能说明:订单详情
     */
    public function userOrderInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id'],

        ];

        $data = $this->model->orderInfo($dis);
        //过期时间减掉现在
        $data['over_time'] -= time();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-16 15:20
     * @功能说明:活动支付
     */
    public function recordPay(){

        $input = $this->_input;

        $dis = [

            'id' => $input['record_id']
        ];

        $record_model = new Record();
        //记录
        $record = $record_model->recordInfo($dis);

        if(empty($record)){

            $this->errorMsg('未找到记录');

        }

        if($record['num']>$record['sign_num']){

            $this->errorMsg('人数不足');

        }

        $class_info = $this->class_model->classInfo(['id'=>$record['class_id'],'status'=>1]);

        if(empty($class_info)){

            $this->errorMsg('视频已经下架');
        }

        //查询配置
        $config_model = new Config();

        $over_time = $config_model->where(['uniacid'=>$this->_uniacid])->value('order_over_time');

        $insert = [

            'uniacid'    => $this->_uniacid,

            'user_id'    => $this->getUserId(),

            'order_code' => orderCode(),

            'init_price' => $class_info['price'],

            'pay_price'  => $class_info['price'],

            'pay_type'   => 1,

            'transaction_id' => '',

            'is_atv'     => $input['record_id'],

            'over_time'  => $over_time+time()
        ];

        Db::startTrans();

        $res = $this->model->orderAdd($insert);

        $id  = $this->model->getLastInsID();

        $order_goods_insert = [

            'uniacid' => $this->_uniacid,

            'order_id'=> $id,

            'class_id'=> $class_info['id'],

            'title'   => $class_info['title'],

            'init_price' => $class_info['original_price'],

            'price'   => $class_info['price'],

            'num'     => 1,

            'cover'   => $class_info['cover']
        ];

        $order_goods = new OrderGoods();

        $order_goods->insert($order_goods_insert);

        Db::commit();

        $pay_controller = new IndexWxPay($this->app);

        $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$this->getUserInfo()['openid'],$this->_uniacid,"购买课程",['type' => 'PayClass' , 'out_trade_no' => $insert['order_code']],$insert['pay_price']);

        return $this->success($jsApiParameters);

    }


}
